home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** Program: Kibitz
- ** File: setup.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1991 Apple Computer, Inc.
- ** All rights reserved.
- */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "Kibitz.h" /* Get the Kibitz includes/typedefs, etc. */
- #include "KibitzCommon.h" /* Get the stuff in common with rez. */
- #include "Kibitz.protos" /* Get the prototypes for Kibitz. */
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- #pragma segment Config
- void DoArrangeBoard(FileRecHndl frHndl, Point clickLoc)
- {
- short piece, fromSq, fromRow, fromCol, toRow, toCol, toSq;
- short palettePiece, color, delta, middle;
- Rect paletteRect, boardRect, squareRect;
- Point releaseLoc;
- long moveAmount;
- RgnHandle dragRgn;
- ControlHandle ctl;
-
- paletteRect = PaletteRect();
- if (PtInRect(clickLoc, &paletteRect)) {
- palettePiece = 1 + (clickLoc.h - paletteRect.left) / kBoardSqSize;
- if (clickLoc.v < paletteRect.top + kBoardSqSize)
- palettePiece = -palettePiece;
- (*frHndl)->doc.palettePiece = palettePiece;
- DrawPalette(frHndl);
- SetOrigin(0, 0);
- return;
- }
-
- SetOrigin(0, 0);
- clickLoc.h += 4096;
-
- boardRect = BoardRect();
- if (PtInRect(clickLoc, &boardRect)) {
-
- fromRow = (clickLoc.v - kBoardVOffset) / kBoardSqSize;
- fromCol = (clickLoc.h - kBoardHOffset) / kBoardSqSize;
-
- squareRect.top = kBoardVOffset + fromRow * kBoardSqSize;
- squareRect.left = kBoardHOffset + fromCol * kBoardSqSize;
- squareRect.bottom = squareRect.top + kBoardSqSize + 2;
- squareRect.right = squareRect.left + kBoardSqSize + 2;
-
- if ((*frHndl)->doc.invertBoard) {
- fromRow = 7 - fromRow;
- fromCol = 7 - fromCol;
- }
- fromSq = START_IBNDS + 10 * fromRow + fromCol;
-
- if ((*frHndl)->doc.theBoard[fromSq] == EMPTY) {
- piece = (*frHndl)->doc.palettePiece;
- if (piece == WP) {
- if (fromRow == 7) return;
- if (fromRow == 0) piece = WQ;
- }
- if (piece == BP) {
- if (fromRow == 0) return;
- if (fromRow == 7) piece = BQ;
- }
- (*frHndl)->doc.theBoard[fromSq] = piece;
- (*frHndl)->doc.gameIndex = 0;
- (*frHndl)->doc.numGameMoves = 0;
- (*frHndl)->fileState.docDirty = true;
- ctl = (*frHndl)->doc.gameSlider;
- (*ctl)->contrlMax = 0;
- (*ctl)->contrlValue = 0;
-
- if (fromSq == 21) (*frHndl)->doc.king[BLACK].rookMoves[QSIDE] = 0;
- if (fromSq == 28) (*frHndl)->doc.king[BLACK].rookMoves[KSIDE] = 0;
- if (fromSq == 91) (*frHndl)->doc.king[WHITE].rookMoves[QSIDE] = 0;
- if (fromSq == 98) (*frHndl)->doc.king[WHITE].rookMoves[KSIDE] = 0;
- /* Adjust castling privileges. */
-
- (*frHndl)->doc.enPasMove = (*frHndl)->doc.arngEnPasMove = 0;
- (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
- /* Double-pawn-push must be last change to enable en-passant. */
-
- ImageDocument(frHndl, true);
- return;
- }
-
- dragRgn = NewRgn();
- RectRgn(dragRgn, &squareRect);
- moveAmount = DragGrayRgn(dragRgn, clickLoc, &boardRect, &boardRect,
- noConstraint, nil);
- DisposeRgn(dragRgn);
-
- if (moveAmount == 0x80008000L) return;
-
- releaseLoc.h = clickLoc.h + (moveAmount & 0xFFFF);
- moveAmount = moveAmount >> 16;
- releaseLoc.v = clickLoc.v + (moveAmount & 0xFFFF);
-
- toRow = (releaseLoc.v - kBoardVOffset) / kBoardSqSize;
- toCol = (releaseLoc.h - kBoardHOffset) / kBoardSqSize;
- if ((*frHndl)->doc.invertBoard) {
- toRow = 7 - toRow;
- toCol = 7 - toCol;
- }
- toSq = START_IBNDS + 10 * toRow + toCol;
-
- if ((*frHndl)->doc.theBoard[toSq] == BK) return;
- if ((*frHndl)->doc.theBoard[toSq] == WK) return;
-
- piece = (*frHndl)->doc.theBoard[fromSq];
- if ((piece == WP) && (toSq >= 91)) return;
- if ((piece == WP) && (toSq <= 28)) piece = WQ;
- if ((piece == BP) && (toSq <= 28)) return;
- if ((piece == BP) && (toSq >= 91)) piece = BQ;
-
- (*frHndl)->doc.theBoard[toSq] = piece;
- (*frHndl)->doc.theBoard[fromSq] = EMPTY;
- (*frHndl)->doc.gameIndex = 0;
- (*frHndl)->doc.numGameMoves = 0;
- (*frHndl)->fileState.docDirty = true;
- ctl = (*frHndl)->doc.gameSlider;
- (*ctl)->contrlMax = 0;
- (*ctl)->contrlValue = 0;
-
- if ((piece == WK) || (piece == BK)) {
- color = (piece < 0) ? WHITE : BLACK;
- (*frHndl)->doc.king[color].kingLoc = toSq;
- (*frHndl)->doc.king[color].kingMoves = true;
- }
-
- if ((fromSq==21) || (toSq==21)) ++(*frHndl)->doc.king[BLACK].rookMoves[QSIDE];
- if ((fromSq==28) || (toSq==28)) ++(*frHndl)->doc.king[BLACK].rookMoves[KSIDE];
- if ((fromSq==91) || (toSq==91)) ++(*frHndl)->doc.king[WHITE].rookMoves[QSIDE];
- if ((fromSq==98) || (toSq==98)) ++(*frHndl)->doc.king[WHITE].rookMoves[KSIDE];
- /* Adjust castling privileges. */
-
- (*frHndl)->doc.enPasMove = (*frHndl)->doc.arngEnPasMove = 0;
- (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
- /* Double-pawn-push must be last change to enable en-passant. */
-
- delta = 0;
- if (piece == WP)
- if ((fromSq >= 81) && (fromSq <= 88))
- delta = fromSq - toSq;
- if (piece == BP)
- if ((fromSq >= 31) && (fromSq <= 38))
- delta = toSq - fromSq;
- if (delta == 20) {
- middle = (fromSq + toSq) / 2;
- if (!(*frHndl)->doc.theBoard[middle]) {
- (*frHndl)->doc.enPasMove = middle;
- (*frHndl)->doc.arngEnPasMove = middle;
- (*frHndl)->doc.enPasPawnLoc = toSq;
- (*frHndl)->doc.arngEnPasPawnLoc = toSq;
- }
- }
-
- ImageDocument(frHndl, true);
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Config
- void DrawPalette(FileRecHndl frHndl)
- {
- short rr, cc, piece;
- Rect paletteRect;
- Handle pieceIcon;
-
- PenSize(2, 2);
- for (rr = 1; rr < 6; ++rr) {
- for (cc = -1; cc < 3; cc += 2) {
- paletteRect = PaletteRect();
- paletteRect.left += (rr - 1) * kBoardSqSize;
- if (cc == 1) paletteRect.top += kBoardSqSize;
- paletteRect.right = paletteRect.left + 32;
- paletteRect.bottom = paletteRect.top + 32;
- piece = rr * cc;
- if (piece == (*frHndl)->doc.palettePiece) piece += 13;
- pieceIcon = GetResource('ICN#', 263 + piece);
- PlotIcon(&paletteRect, pieceIcon);
- InsetRect(&paletteRect, -3, -3);
- FrameRect(&paletteRect);
- }
- }
- PenNormal();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment Config
- Rect PaletteRect(void)
- {
- Rect paletteRect;
-
- paletteRect.top = 74;
- paletteRect.left = kBoardWidth + 21;
- paletteRect.right = paletteRect.left + 5 * kBoardSqSize;
- paletteRect.bottom = paletteRect.top + 2 * kBoardSqSize;
- OffsetRect(&paletteRect, -4096, 0);
-
- return(paletteRect);
- }
-
-
-
-